//
//  SettingTableViewController.m
//  Car
//
//  Created by lanou on 15/10/5.
//  Copyright (c) 2015年 朱鸿儒. All rights reserved.
//

#import "SettingTableViewController.h"
#import "FontTableViewCell.h"
#import "SwitchTableViewCell.h"
#import "OtherTableViewCell.h"
#import "CacheTool.h"

@interface SettingTableViewController ()<UIAlertViewDelegate>
@property (nonatomic, assign) float size;
@end

@implementation SettingTableViewController

/**
 *  计算缓存大小
 */
- (void)viewWillAppear:(BOOL)animated {
	[super viewWillAppear:YES];
    self.size = [CacheTool folderSizeAtPath:[self getCachesPath]];
    [self.tableView reloadData];
}
/**
 *  获取缓存路径
 */
- (NSString *)getCachesPath {
    NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    return filePath;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUpData];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)setUpData{
    [self.tableView registerClass:[FontTableViewCell class] forCellReuseIdentifier:@"fontCell"];
    [self.tableView registerClass:[SwitchTableViewCell class] forCellReuseIdentifier:@"switchCell"];
    [self.tableView registerClass:[OtherTableViewCell class] forCellReuseIdentifier:@"cell"];
    self.tableView.separatorStyle = 0;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 4;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
    FontTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"fontCell" forIndexPath:indexPath];
        cell.selectionStyle = 0;
        return cell;
    }
    if (indexPath .row == 1 ) {
    SwitchTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"switchCell" forIndexPath:indexPath];
        [cell.sw addTarget:self action:@selector(picChange:) forControlEvents:(UIControlEventValueChanged)];
        if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"isOnPic"]isEqualToString:@"YES"]) {
            [Setting shareWithSetting].isOnPic = YES;
            [cell.sw setOn:YES];
        }
        
        
        cell.selectionStyle = 0;
        return cell;
    }
    if (indexPath .row == 2 ) {
        SwitchTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"switchCell" forIndexPath:indexPath];
        [cell setWithString:@"夜间模式"];
        [cell.sw addTarget:self action:@selector(nightChange:) forControlEvents:(UIControlEventValueChanged)];
        if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"isOnNight"]isEqualToString:@"YES"]) {
            [Setting shareWithSetting].isOnNight = YES;
            [cell.sw setOn:YES];
        }

        cell.selectionStyle = 0;
        return cell;
    }
    if (indexPath .row == 3) {
        OtherTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        cell.numLable.text = [NSString stringWithFormat:@"%.2fM",self.size];
        cell.selectionStyle = 0;
        return cell;
    }else
        return 0;
    
}
- (void)picChange:(UISwitch *)sw{
    [Setting shareWithSetting].isOnPic = sw.isOn;
    NSUserDefaults *setting = [NSUserDefaults standardUserDefaults];
    if (sw.isOn == YES)
    [setting setObject:@"YES" forKey:@"isOnPic"];
    else
        [setting removeObjectForKey:@"isOnPic"];
}
-(void)nightChange:(UISwitch *)sw{
    [Setting shareWithSetting].isOnNight = sw.isOn;
    NSUserDefaults *setting = [NSUserDefaults standardUserDefaults];
    if (sw.isOn == YES)
        [setting setObject:@"YES" forKey:@"isOnNight"];
    else
        [setting removeObjectForKey:@"isOnNight"];
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 3) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"清除缓存" message:[NSString stringWithFormat:@"清除%.2fm缓存",self.size] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alert show];
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 45;
}
#pragma mark <-<-<-<-------alert代理------->->->->
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 1) {
        [CacheTool clearCache:[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]];
        self.size = [CacheTool folderSizeAtPath:[self getCachesPath]];
        [self.tableView reloadData];
    }
    
}

@end
